<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  install
 */

class m_install_actions_DatabaseAction extends f_core_Action
{
	public function execute()
	{
        $formValues = $this->getFormValues('form');
        if(!is_null($formValues))
        {
            $config = f_loader_Config::getInstance();

            $config->setParameter('DBConfig', 'host', $formValues['host']);
            $config->setParameter('DBConfig', 'login', $formValues['login']);
            $config->setParameter('DBConfig', 'password', $formValues['password']);
            $config->setParameter('DBConfig', 'dbName', $formValues['dbName']);

            $config->save();

            if(!f_database_Provider::checkConnect())
            {
                $this->addToModel('error', true);
                $this->addToModel('value', $formValues);
                $this->setView('input');
            }
            else
            {
                m_install_services_InstallService::getInstance()->initDb();
                f_package_Manager::getInstance()->upgradeAllPAckages();
                $this->addToModel('config', f_loader_Config::getInstance()->getParameters('DBConfig'));
                f_core_Controller::getInstance()->redirect(URL_RELATIVE.'/bo/install/User');
            }
        }
        else
        {
            $this->setView('input');
        }
	}

	public function validate()
	{
        if(f_loader_Config::getInstance()->getParameter('default', 'defaultDocumentLang') == '')
        {
            return false;
        }

		return !f_database_Provider::checkConnect();
	}

	public function errorHandler()
	{
        if(f_loader_Config::getInstance()->getParameter('default', 'defaultDocumentLang') == '')
        {
            $this->addToModel('error', 'lang');
        }
        else
        {
            $this->addToModel('error', 'already');
        }
        
        $this->setView('error');
	}
}